Skip to content

drivers: timer: nrf_rtc_timer: Allow use of custom bit width #92025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mstasiaknordic
Copy link
Contributor

Allowed use of counter bit width lower than hardware 24. In that case, PPI connection is established to trigger clear task once maximum value is reached.

@bjarki-andreasen
Copy link
Contributor

Out of curiosity, what usecases are there for artificially lowering the counters bit widht? Only effect I can think of is more frequent wakeups to account for rollover, which would be worse for power consumption :)

@mstasiaknordic mstasiaknordic force-pushed the rtc_custom_bitwidth branch 3 times, most recently from d40e414 to 35cf7eb Compare July 2, 2025 08:31
Allowed use of counter bit width lower than hardware 24.
In that case, PPI connection is established to trigger
clear task once maximum value is reached.

Signed-off-by: Michał Stasiak <michal.stasiak@nordicsemi.no>
Added overlay for the board.

Signed-off-by: Michał Stasiak <michal.stasiak@nordicsemi.no>
Copy link

config NRF_RTC_COUNTER_BIT_WIDTH
int
default 15 if SOC_NRF52832
default 24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

range x 24

@@ -17,11 +17,21 @@
#include <haly/nrfy_rtc.h>
#include <zephyr/irq.h>

/* Ensure that selected counter bit width is within its maximum hardware width. */
BUILD_ASSERT(CONFIG_NRF_RTC_COUNTER_BIT_WIDTH <= 24, "Counter bit width exceeds maximum width.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
BUILD_ASSERT(CONFIG_NRF_RTC_COUNTER_BIT_WIDTH <= 24, "Counter bit width exceeds maximum width.");
BUILD_ASSERT(CONFIG_NRF_RTC_COUNTER_BIT_WIDTH <= RTC_BIT_WIDTH, "Counter bit width exceeds maximum width.");

If range is added in Kconfig then it is redundant.

/* Ensure that selected counter bit width is within its maximum hardware width. */
BUILD_ASSERT(CONFIG_NRF_RTC_COUNTER_BIT_WIDTH <= 24, "Counter bit width exceeds maximum width.");

#if (CONFIG_NRF_RTC_COUNTER_BIT_WIDTH < 24)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#if (CONFIG_NRF_RTC_COUNTER_BIT_WIDTH < 24)
#if (CONFIG_NRF_RTC_COUNTER_BIT_WIDTH < RTC_BIT_WIDTH)

@@ -33,7 +43,7 @@ BUILD_ASSERT(CHAN_COUNT <= CHAN_COUNT_MAX, "Not enough compare channels");
BUILD_ASSERT(DT_NODE_HAS_STATUS(DT_NODELABEL(RTC_LABEL), disabled),
"Counter for RTC1 must be disabled");

#define COUNTER_BIT_WIDTH 24U
#define COUNTER_BIT_WIDTH CONFIG_NRF_RTC_COUNTER_BIT_WIDTH
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be good to have
#define RTC_BIT_WIDTH 24

@@ -560,8 +590,13 @@ void rtc_nrf_isr(const void *arg)
rtc_pretick_rtc1_isr_hook();
}

if (nrfy_rtc_int_enable_check(RTC, NRF_RTC_INT_OVERFLOW_MASK) &&
nrfy_rtc_events_process(RTC, NRF_RTC_INT_OVERFLOW_MASK)) {
if ((nrfy_rtc_int_enable_check(RTC, NRF_RTC_INT_OVERFLOW_MASK) &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to check OVERFLOW event if custom bit width is used.

@@ -742,7 +781,7 @@ static int sys_clock_driver_init(void)

int_mask = BIT_MASK(CHAN_COUNT);
if (CONFIG_NRF_RTC_TIMER_USER_CHAN_COUNT) {
alloc_mask = BIT_MASK(EXT_CHAN_COUNT) << 1;
alloc_mask = BIT_MASK(EXT_CHAN_COUNT) << (1 + CUSTOM_COUNTER_BIT_WIDTH);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With potentially 2 internal channels It would be good to define

#define SYS_CLOCK_CH 0
#define WRAP_CH 1

And use it here and in places where 0 and 1 are used explicitly now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants